package com.quectel.cms.controller.parkingcard;


import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.parkingcard.dto.ParkingCardDto;
import com.quectel.core.module.parkingcard.service.ParkingCardService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/04/18 17:23
 */
@RestController
@RequestMapping("parkingCard")
@Tag(name = "ParkingCardController", description = "停车月卡相关api")
public class ParkingCardController extends BaseController {

    @DubboReference
    private ParkingCardService parkingCardService;

    /**
     * 查询全部
     *
     * @param villageId
     * @return
     */
    @GetMapping("queryAll")
    @Operation(summary = "查询全部")
    public Response<List<ParkingCardDto>> queryAll(
            @Parameter(description = "小区id") @RequestParam Long villageId
    ) {
        //查询列表数据
        Request request = Request.configParamsNotPage(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());

        request.put(LambdaUtil.getFieldName(ParkingCardDto::getVillageId), villageId);
        request.put(LambdaUtil.getFieldName(ParkingCardDto::getStatus), SystemConstants.NORMAL);
        int total = parkingCardService.queryTotal(request.getParams());

        List<ParkingCardDto> list = null;
        if (total > 0) {
            list = parkingCardService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        return Response.<List<ParkingCardDto>>ok().wrap(list);
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "parkingCard:list")
    @Operation(summary = "分页列表")
    public Response<Page<ParkingCardDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = parkingCardService.queryTotal(request.getParams());

        List<ParkingCardDto> list = null;
        if (total > 0) {
            list = parkingCardService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<ParkingCardDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<ParkingCardDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "parkingCard:edit")
    @Operation(summary = "查询单条记录")
    public Response<ParkingCardDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (parkingCardService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        ParkingCardDto parkingCardDto = parkingCardService.selectById(id);

        return Response.<ParkingCardDto>ok().wrap(parkingCardDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "parkingCard:info")
    @Operation(summary = "查询单条记录")
    public Response<ParkingCardDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (parkingCardService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        ParkingCardDto parkingCardDto = parkingCardService.selectById(id);

        return Response.<ParkingCardDto>ok().wrap(parkingCardDto);
    }

    /**
     * 保存
     *
     * @param parkingCardDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "parkingCard:edit")
    @Operation(summary = "保存单条记录")
    public Response<Object> save(@RequestBody @Validated ParkingCardDto parkingCardDto) {

        //填充基础数据
        fillSaveCommonData(parkingCardDto);

        parkingCardService.save(parkingCardDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param parkingCardDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "parkingCard:edit")
    @Operation(summary = "修改单条记录")
    public Response<Object> update(@RequestBody @Validated ParkingCardDto parkingCardDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(parkingCardDto.getId()));
        if (parkingCardService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(parkingCardDto);

        parkingCardService.updateById(parkingCardDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "parkingCard:delete")
    @Operation(summary = "批量删除记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (parkingCardService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        parkingCardService.deleteBatch(ids);

        return Response.ok();
    }
}
